


#ifndef _MY_MUTEX_H_
#define _MY_MUTEX_H_

#define OS_LINUX 1


//////////////////////////////////////////////////////////////////////////
//Mutex
//////////////////////////////////////////////////////////////////////////

class clMutex
{
public:
	clMutex()
	{
#ifdef OS_LINUX
		pthread_mutex_init( &TheMutex, NULL );
#endif
#ifdef OS_WINDOWS
		InitializeCriticalSection( &TheCS );
#endif
	}

	/// Enter the critical section -- other threads are locked out
	void Lock() const
	{
#ifdef OS_LINUX
		pthread_mutex_lock( &TheMutex );
#endif
#ifdef OS_WINDOWS

		if ( !TryEnterCriticalSection( &TheCS ) ) EnterCriticalSection( &TheCS );
#endif
	}

	/// Leave the critical section
	void Unlock() const
	{
#ifdef OS_LINUX
		pthread_mutex_unlock( &TheMutex );
#endif
#ifdef OS_WINDOWS
		LeaveCriticalSection( &TheCS );
#endif
	}

	~clMutex()
	{
#ifdef OS_WINDOWS
		DeleteCriticalSection( &TheCS );
#endif
#ifdef OS_LINUX
		pthread_mutex_destroy( &TheMutex );
#endif
	}

#ifdef OS_LINUX
	// POSIX threads
	mutable pthread_mutex_t TheMutex;
#endif
#ifdef OS_WINDOWS
	mutable CRITICAL_SECTION TheCS;
#endif
};

#endif